package org.dtrd.modules.evaluation.controller;

import cn.hutool.core.util.ArrayUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.dtrd.base.BaseController;
import org.dtrd.common.api.vo.Result;
import org.dtrd.modules.base.service.BaseCommonService;
import org.dtrd.modules.evaluation.constant.Evaluation;
import org.dtrd.modules.evaluation.entity.bean.BmqInfo;
import org.dtrd.modules.evaluation.entity.dto.BmqDTO;
import org.dtrd.modules.evaluation.service.IDtrdEntEvaluationRdBmqService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.List;

import static org.dtrd.common.constant.CommonConstant.OPERATE_TYPE_DELETE;
import static org.dtrd.common.constant.CommonConstant.OPERATE_TYPE_QUERY;
import static org.dtrd.config.system.SystemState.ResponseState.*;

/**
 * @Author: QR
 * @Date: 2021/9/27-9:45
 */
@Api(tags = "量表评估")
@RestController
public class BmqController extends BaseController {

    @Autowired
    private IDtrdEntEvaluationRdBmqService bmqService;
    @Autowired
    private BaseCommonService baseCommonService;

    @ApiOperation("保存BMQ(服药信念评估)量表记录")
    @PostMapping(value = {"/105/02/1050201", "/100/17/1001706"})
    public Result<?> saveBMQInfo(@RequestBody BmqDTO dto){
        if (ArrayUtil.isEmpty(dto.getAnswers())) {
            return Result.error(STATE_PARAMS_MISSING.getCode(), STATE_PARAMS_MISSING.getMsg());
        }
        if (dto.getPatientId() <= 0 || dto.getAnswers().length != Evaluation.ProblemNum.BMQ.getNum()
                || ArrayUtil.hasEmpty((Object) dto.getAnswers())) {
            return Result.error(STATE_PARAMS_ERROR.getCode(), STATE_PARAMS_ERROR.getMsg());
        }
        BmqInfo bmqInfo = bmqService.saveBMQInfo(dto);
        String logContent = String.format("保存BMQ(服药信念评估)量表记录, 保存用户id：%d", dto.getPatientId());
        baseCommonService.addLog(logContent, OPERATE_TYPE_QUERY);
        return Result.OK(bmqInfo);
    }

    @ApiOperation("获取最近一次BMQ(服药信念评估)")
    @GetMapping("/105/02/1050202")
    public Result<?> getBMQInfoLatest(@ApiParam("患者id") @PathParam("patientId") Integer patientId){
        if (patientId == null) {
            return Result.error(STATE_PARAMS_MISSING.getCode(), STATE_PARAMS_MISSING.getMsg());
        }
        if (patientId <= 0) {
            return Result.error(STATE_PARAMS_ERROR.getCode(), STATE_PARAMS_ERROR.getMsg());
        }
        BmqInfo bmqInfo = bmqService.getBMQInfoLatest(patientId);
        String logContent = String.format("获取最近一次BMQ(服药信念评估)，查询用户id：%d", patientId);
        baseCommonService.addLog(logContent, OPERATE_TYPE_QUERY);
        return Result.OK(bmqInfo);
    }

    @ApiOperation("获取bmq列表")
    @GetMapping("/105/02/1050203")
    public Result<?> getBMQList(@RequestParam("patientId") Integer patientId){
        if (isIntParamInvalid(patientId)) {
            return error(STATE_PARAMS_MISSING);
        }
        List<BmqInfo> bmqInfoList = bmqService.getBmqInfoList(patientId);
        String logContent = String.format("获取bmq列表，查询用户id：%d", patientId);
        baseCommonService.addLog(logContent, OPERATE_TYPE_QUERY);
        return Result.OK(bmqInfoList);
    }

    @ApiOperation("删除bmq记录")
    @DeleteMapping("/105/02/1050204")
    public Result<?> removeBMQById(@RequestParam("dataId") Integer dataId){
        if (isIntParamInvalid(dataId)){
            return error(STATE_PARAMS_MISSING);
        }
        boolean result = bmqService.removeById(dataId);
        String logContent = String.format("删除bmq记录，删除量表记录id：%d", dataId);
        baseCommonService.addLog(logContent, OPERATE_TYPE_DELETE);
        return result ? Result.OK() : error(STATE_OPERATION_FAILURE);
    }
}
